
#include <librally.h>

Err rp_score_init();
Err rp_score_finalize();
Err rp_score_set();
Err rp_score_get();

// ==============================================

Err rp_score_init(Score *score)
{
	char fname[] = "rp_score_init";
	char msg[] = "fatal error";
	Err err;
	FLOG_IN(fname);
	
	err = rp_sem_init(&(score->sem), SEM_PRIVATE, 1);
	score->heartbeat = 0;
	score->code = OP_NONE;
	score->state = SS_IDLE;

	FLOG_OUT(fname);
	return err;
}

Err rp_score_init_pshared(Score *score)
{
	char fname[] = "rp_score_init";
	char msg[] = "fatal error";
	Err err;

	err = rp_sem_init(&(score->sem), SEM_SHARED, 1);
	err = rp_sem_wait(&(score->sem));
	score->heartbeat = 0;
	score->code = OP_NONE;
	score->state = SS_IDLE;
	err = rp_sem_post(&(score->sem));
	
	return err;
}

Err rp_score_finalize(Score *score)
{
	char fname[] = "rp_score_finalize";
	char msg[] = "fatal error";
	Err err;
	
	err = rp_sem_destroy(&(score->sem));

	return err;
}

Err rp_score_set(Score *score, int *heartbeat, OpCode *code, State *state)
{
	char fname[] = "rp_score_get";
	char msg[] = "fatal error";
	Err err;

	err =  rp_sem_wait(&(score->sem));
	if (heartbeat)	 score->heartbeat = *heartbeat;
	if (code) 	   	 score->code = *code;
	if (state) 		 score->state = *state;
	err = rp_sem_post(&(score->sem));

	return err;
}

Err rp_score_get(Score *score, int *heartbeat, OpCode *code, State *state)
{
	char fname[] = "rp_score_get";
	char msg[] = "fatal error";
	Err err;

	err = rp_sem_wait(&(score->sem));
	if (heartbeat != NULL)	 *heartbeat = score->heartbeat;
	if (code != NULL) 	   	 *code = score->code;
	if (state != NULL) 		 *state = score->state;
	err = rp_sem_post(&(score->sem));

	return err;
}

int rp_score_get_heartbeat(Score *score)
{
	char fname[] = "rp_score_get_heartbeat";
	char msg[] = "fatal error";
	Err err;
	int heartbeat;

	err = rp_score_get(score, &heartbeat, NULL, NULL);

	return heartbeat;
}

Err rp_score_set_heartbeat(Score *score, int heartbeat)
{
	char fname[] = "rp_score_set_heartbeat";
	char msg[] = "fatal error";
	Err err;

	err = rp_score_set(score, &heartbeat, NULL, NULL);

	return err;
}

OpCode rp_score_get_code(Score *score)
{
	char fname[] = "rp_score_get_code";
	char msg[] = "fatal error";
	Err err;
	OpCode code;

	err = rp_score_get(score, NULL, &code, NULL);

	return code;
}

Err rp_score_set_code(Score *score, OpCode code)
{
	char fname[] = "rp_score_set_code";
	char msg[] = "fatal error";
	Err err;

	err = rp_score_set(score, NULL, &code, NULL);

	return err;
}

State rp_score_get_state(Score *score)
{
	char fname[] = "rp_score_get_state";
	char msg[] = "fatal error";
	Err err;
	State state;

	err = rp_score_get(score, NULL, NULL, &state);

	return state;
}

Err rp_score_set_state(Score *score, State state)
{
	char fname[] = "rp_score_set_state";
	char msg[] = "fatal error";
	Err err;

	err = rp_score_set(score, NULL, NULL, &state);

	return err;
}


